<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class Profile extends Controller {

    function Profile() {
        parent::Controller();
    }

    function index() {
        //tampilin Profile form
    }
    
    function home(){
        $u = $this->session->userdata('username'); 
        $data['page'] = 'profile';
        $this->load->model('user_model','usrmdl', TRUE);
        $data['user_details'] = $this->usrmdl->get_user_details($u);
        $this->load->view('layout/main', $data);
    }
    
    function edit_members($action){
        $this->load->model('user_model','usrm', TRUE);
        if ($action == 'view'){
            //load view yg nampilin nama + change name
            $data['page'] = 'edit_member';
            $data['user'] = $this->usrm->get_user_details($this->session->userdata('username'));
            $data['form'] = $this->form();
            $this->load->view('layout/main', $data);
            
        }
        else if ($action == 'submit'){
            
            if ($this->input->post('new_membername_1') != null){
                $this->db->where('username', $this->session->userdata('username'));
                $update = array(
                    'member_name_1' => $this->input->post('new_membername_1')
                );

                $this->db->update('team_members', $update);
                redirect(base_url(). index_page(). '/profile/home', 'location');
            }
            if ($this->input->post('new_membername_2') != null){
                $this->db->where('username', $this->session->userdata('username'));
                $update = array(
                    'member_name_2' => $this->input->post('new_membername_2')
                );

                $this->db->update('team_members', $update);
                redirect(base_url(). index_page(). '/profile/home', 'location');
            }
            if ($this->input->post('new_membername_3') != null){
                $this->db->where('username', $this->session->userdata('username'));
                $update = array(
                    'member_name_3' => $this->input->post('new_membername_3')
                );

                $this->db->update('team_members', $update);
                redirect(base_url(). index_page(). '/profile/home', 'location');
            }
            if ($this->input->post('new_membername_4') != null){
                $this->db->where('username', $this->session->userdata('username'));
                $update = array(
                    'member_name_4' => $this->input->post('new_membername_4')
                );

                $this->db->update('team_members', $update);
                redirect(base_url(). index_page(). '/profile/home', 'location');
            }
        }
    }
    
    function change_password(){
        $data['page'] = 'changepwd';
        $data['fail'] = false;
        $data['success'] = false;
        $data['currpwd'] = array(
           'id' => 'currpwd',
           'name' => 'currpwd'
        );
        $data['newpwd'] = array(
           'id' => 'newpwd',
           'name' => 'newpwd'
        );
        $this->load->view('layout/main', $data);
        
    }
    
    
    function submit_newpwd(){
        $u = $this->session->userdata('username');
        //ambil password dari db terus dibandingin
        $this->load->model('user_model','usrmdl', TRUE);
        $user = $this->usrmdl->get_user_details($u);
        
        $currpwd = sha1($this->input->post('currpwd'));
        $newpwd = sha1($this->input->post('newpwd'));
        
        if ($currpwd == $user['details']['password']){
            $this->db->where('username', $u);
            $update = array('password' => $newpwd);
            $this->db->update('users', $update);
            $data['page'] = 'changepwd';
            $data['fail'] = false;
            $data['success'] = true;
            $data['currpwd'] = array(
               'id' => 'currpwd',
               'name' => 'currpwd'
            );
            $data['newpwd'] = array(
               'id' => 'newpwd',
               'name' => 'newpwd'
            );
            $this->load->view('layout/main', $data);
        }
        else{
            //pesan error
            $data['page'] = 'changepwd';
            $data['fail'] = true;
            $data['success'] = false;
            $data['currpwd'] = array(
               'id' => 'currpwd',
               'name' => 'currpwd'
            );
            $data['newpwd'] = array(
               'id' => 'newpwd',
               'name' => 'newpwd'
            );
            $this->load->view('layout/main', $data);
        }
    }
    
    function form() {
        $data['new_membername_1'] = array(
            'id' => 'new_membername_1',
            'name' => 'new_membername_1'
            
        );
        $data['new_membername_2'] = array(
            'id' => 'new_membername_2',
            'name' => 'new_membername_2'
            
        );
        $data['new_membername_3'] = array(
            'id' => 'new_membername_3',
            'name' => 'new_membername_3'
            
        );
        $data['new_membername_4'] = array(
            'id' => 'new_membername_4',
            'name' => 'new_membername_4'
            
        );
        return $data;
    }
}
?>
